﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ImageProcessing
{
	public partial class ConformalDialog : Form
	{
		Bitmap bmIn;
		Bitmap bmOut;
		Bitmap bmModTh;
		Bitmap bmBaseTh;
		const int THUMBNAILSIZE = 300;
		string[] asEffects = { "Pixelation", "Ripple", "Twirl", "Jitter", "CircularSlice" };
		bool imageReady;
		bool isChanged = false;
		double[, ,] adImage;
		double[, ,] adImageTh;
		double[, ,] adImageModTh;


		delegate double[, ,] delConformal(double[, ,] adImage, int nImageWidth, int nImageHeight, double[] adParams, int nNumArgs);
		Dictionary<string, delConformal> dictOperations;
		delConformal conformalOperation;


		#region Properties

		public Bitmap ImageIn
		{
			get { return bmIn; }
			set
			{
				bmIn = new Bitmap(value);
				//size thumbnail picboxes to proper size
				picBoxBase.Size = new Size(THUMBNAILSIZE, THUMBNAILSIZE);
				picBoxModified.Size = new Size(THUMBNAILSIZE, THUMBNAILSIZE);
				bmBaseTh = (Bitmap)value.GetThumbnailImage(THUMBNAILSIZE, THUMBNAILSIZE, null, IntPtr.Zero);
				bmModTh = new Bitmap(bmBaseTh);

				//normalize image for modifications
				this.Cursor = Cursors.WaitCursor;
				adImage = BitmapConversions.NormalizeRGB(bmIn);
				adImageTh = BitmapConversions.NormalizeRGB(bmBaseTh);
				this.Cursor = Cursors.Default;
				imageReady = true;
			}
		}

		public Bitmap ImageOut
		{
			get { return bmOut; }
			set { bmOut = new Bitmap(value); }
		}
		#endregion Properties


		public ConformalDialog()
		{
			InitializeComponent();
		}

		public ConformalDialog(Bitmap bmin)
		{
			InitializeComponent();

			ImageIn = bmin;
			dictOperations = new Dictionary<string, delConformal>();
			FillConformalCombo();
		
			imageReady = true;
		}

		private void FillConformalCombo()
		{
			conformalOperation = ConformalMappings.Conformal1;
			dictOperations.Add("Conformal1", conformalOperation);
			conformalOperation = ConformalMappings.ConformalComplex1;
			dictOperations.Add("Complex1", conformalOperation);
			conformalOperation = ConformalMappings.ConformalComplex2;
			dictOperations.Add("Complex2", conformalOperation);
			conformalOperation = ConformalMappings.ConformalComplex3;
			dictOperations.Add("Complex3", conformalOperation);
			KeyValuePair<string, delConformal>[] kvpFunctions = dictOperations.ToArray();

			cmbboxConformal.DataSource = kvpFunctions;
			cmbboxConformal.DisplayMember = "Key";
		}

		private void btnDoMapping_Click(object sender, EventArgs e)
		{
			adImageModTh = dictOperations[cmbboxConformal.Text](adImageTh, THUMBNAILSIZE, THUMBNAILSIZE, new double[] { Convert.ToDouble(txtboxP1.Text), Convert.ToDouble(txtboxP2.Text)}, 2);
			bmModTh = BitmapConversions.UnNormalizeRGB(adImageModTh, THUMBNAILSIZE, THUMBNAILSIZE, true, bmIn.PixelFormat);
			isChanged = true;
			picBoxModified.Invalidate();

		}

		#region OnPaint
		private void picBoxModified_Paint(object sender, PaintEventArgs e)
		{
			if (imageReady)
				picBoxModified.BackgroundImage = bmModTh;
		}

		private void picBoxBase_Paint(object sender, PaintEventArgs e)
		{
			if (imageReady)
				picBoxBase.BackgroundImage = bmBaseTh;
		}
		#endregion OnPaint

		private void btnOk_Click(object sender, EventArgs e)
		{
			if (isChanged)
			{
				adImage = dictOperations[cmbboxConformal.Text](adImage, bmIn.Width, bmIn.Height, new double[] { Convert.ToDouble(txtboxP1.Text),Convert.ToDouble(txtboxP2.Text) }, 2);
				ImageOut = BitmapConversions.UnNormalizeRGB(adImage, bmIn.Width, bmIn.Height, chkboxClamp.Checked, bmIn.PixelFormat);
			}

			else
				ImageOut = bmIn;
		}
	}
}
